package com.xwj.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xwj.back.mapper.RoleMenuMapper;
import com.xwj.back.pojo.RoleMenu;
import com.xwj.back.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;


    @Override
    public Map<String, String> updateRoleMenu(String role,Integer roleId, List<Integer> menuIds,List<Integer> half) {
        Map<String, String> map = new HashMap<>();
        if(!"ROLE_ADMIN".equals(role)) {
            map.put("message", "error");
            return map;
        }
        roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("role_id",roleId));
        for (Integer menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu(roleId, menuId);
            roleMenuMapper.insert(roleMenu);
        }
        for (Integer menuId : half) {
            RoleMenu roleMenu = new RoleMenu(roleId, menuId);
            roleMenuMapper.insert(roleMenu);
        }
        map.put("message","success");
        return map;
    }
}
